package org.come.login;

import com.tool.tcpimg.UIUtils;
import org.come.bean.ConfigureBean;
import org.come.bean.LoginRoleInfo;
import org.come.bean.LoginUserBean;
import org.come.model.Configure;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.test.Main;
import org.come.until.GsonUtil;
import org.come.until.UserData;
import org.come.until.UserMessUntil;
import org.come.until.Util;
import org.come.view.View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.ImageObserver;
import java.math.BigDecimal;
import java.util.Map;

public class LoginView extends View {
  private SpriteBtn btnLogopic;
  private SpriteBtn btnCancel;
  private SpriteBtn btnExit;
  private SpriteBtn btnLogin;
  private SpriteBtn btnRegister;
  private SpriteBtn btnRemId;
  private SpriteBtn btnPwdPro;
  private SpriteBtn btnDropdown;
  private SpriteBtn btnChangePwd;
  private ImageIcon background;
  private SpriteBtn btngouzi1;
  private SpriteBtn btngouzi2;
  private JTextField userAccount;
  private JPasswordField textPasswordField;
  private int jiaodian;
  private KeyView keyView;
  private RegisterView registerView;
  private IphoneView iphoneView;
  private DownView downView;
  private String msg;


  public LoginView(LoginJpanel loginJpanel) {
    setBounds(0, 0, 1024, 720);

    this.background = new ImageIcon("resource/NewUi/登陆界面.png");
    
    this.btnLogopic = new SpriteBtn("resource/NewUi/logo", 363, 100, false);
    this.btnLogopic.setBounds(363, 100, 800, 600);
    add(this.btnLogopic);
    //记住密码框
    this.btnRemId = new SpriteBtn("resource/NewUi/按钮_勾选框新", 396, 376, true);
    this.btnRemId.setBounds(396, 376, 24, 24);
    this.btnRemId.addMouseListener(new LoginMouslisten(0, this.btnRemId, loginJpanel));
    add(this.btnRemId);
    //密码保护框
    this.btnPwdPro = new SpriteBtn("resource/NewUi/小键盘", 579, 341, true);
    this.btnPwdPro.setBounds(586, 342, 19, 19);
    this.btnPwdPro.addMouseListener(new LoginMouslisten(1, this.btnPwdPro, loginJpanel));
    add(this.btnPwdPro);
    
    this.btnDropdown = new SpriteBtn("resource/NewUi/下拉按钮新", 583, 315, false);
    this.btnDropdown.setBounds(585, 315, 19, 19);
    this.btnDropdown.addMouseListener(new LoginMouslisten(4, this.btnDropdown, loginJpanel));
    add(this.btnDropdown);

    //进入
    this.btnLogin = new SpriteBtn("resource/NewUi/进入游戏_按钮", 422, 394, false);
    this.btnLogin.addMouseListener(new LoginMouslisten(5, this.btnLogin, loginJpanel));
    add(this.btnLogin);

    this.btnCancel = new SpriteBtn("resource/NewUi/返回按钮全新", 350, 271, false);
    this.btnCancel.setBounds(350, 271, 0, 0);
    this.btnCancel.addMouseListener(new MainMouslisten(1, this.btnCancel, loginJpanel));;
    add(this.btnCancel);
    
    this.btnRegister = new SpriteBtn("resource/NewUi/注册", 600, 268, false);
    this.btnRegister.setBounds(600, 268, 45, 18);
    this.btnRegister.addMouseListener(new LoginMouslisten(2, this.btnRegister, loginJpanel));
    add(this.btnRegister);
    
//    this.btnExit = new SpriteBtn("resource/loginButton/EBD05656.was", 450, 271, false);
//    this.btnExit.setBounds(450, 271, 0, 0);
//    this.btnExit.addMouseListener(new MainMouslisten(5, this.btnExit, loginJpanel));
//    add(this.btnExit);
    
    this.userAccount = new JTextField();
    this.userAccount.setBounds(403, 315, 185, 23);
    this.userAccount.setOpaque(false);
    this.userAccount.setBorder(BorderFactory.createEmptyBorder());
    this.userAccount.setForeground(Color.BLACK);
    this.userAccount.setFont(new Font("楷体", 0, 15));
    this.userAccount.setCaretColor(Color.BLACK);
    this.userAccount.addFocusListener(new FocusListener() {
      @Override
      public void focusLost(FocusEvent e) {}
      
      @Override
      public void focusGained(FocusEvent e)
      {
        LoginView.this.jiaodian = 1;
      }
    });
    add(this.userAccount);
    this.textPasswordField = new JPasswordField();
    this.textPasswordField.setBounds(403, 347, 185, 23);
    this.textPasswordField.setOpaque(false);
    this.textPasswordField.setBorder(BorderFactory.createEmptyBorder());
    this.textPasswordField.setForeground(Color.BLACK);
    this.textPasswordField.setFont(new Font("楷体", 0, 15));
    this.textPasswordField.setEchoChar('*');
    this.textPasswordField.setCaretColor(Color.BLACK);
    this.textPasswordField.addFocusListener(new FocusListener() {
      @Override
      public void focusLost(FocusEvent e) {}
      
      @Override
      public void focusGained(FocusEvent e)
      {
        LoginView.this.jiaodian = 2;
      }
    });
    add(this.textPasswordField);

    this.cshtext();

  }
  
  public void cshtext() {
    String v = Util.readUserPwd();
    String[] vs = v.split("\\|");
    String[] vs1 = vs[0].split("=");
    if (vs1.length == 2) {
      this.userAccount.setText(vs1[0]);
      this.textPasswordField.setText(vs1[1]);
    }
  }
  
  public void cshtext(String text) {
    String[] vs = text.split("=");
    this.userAccount.setText(vs[0]);
    this.textPasswordField.setText(vs[1]);
    this.downView.setVisible(false);
  }
  
  public void addtext(String sr) {
    if (this.jiaodian == 1) {
      this.userAccount.setText(this.userAccount.getText() + sr);
    } else if (this.jiaodian == 2) {
      this.textPasswordField.setText(this.textPasswordField.getText() + sr);
    }
  }
  
  public void jilu(String userandpwd) {
    if (this.btngouzi1 != null) {
      String v = Util.readUserPwd();
      String[] vs = v.split("\\|");
      if (vs.length >= 6) {
        v = UserData.Splice(v, vs[0], 4);
      }
      Util.writeUserPwd(UserData.Splice(UserData.Splice(v, userandpwd, 4), userandpwd, 0));
    }
  }
  
  public void login() {
    this.msg = null;
    LoginUserBean loginUserBean = new LoginUserBean();
    if (("".equals(this.userAccount.getText())) || ("".equals(this.textPasswordField.getText()))) {
      this.msg = "账号/密码不能为空！";
    }
    loginUserBean.setUsername(this.userAccount.getText());
    String pwd = new String(this.textPasswordField.getText());
    loginUserBean.setPassword(pwd);
    if ((!"".equals(loginUserBean.getUsername())) && (!"".equals(loginUserBean.getPassword()))) {
      jilu(this.userAccount.getText() + "=" + this.textPasswordField.getText());
      
      String mesage = Agreement.getAgreement().LoginAgreement(GsonUtil.getGsonUtil().getgson().toJson(loginUserBean));
      SendMessageUntil.toServer(mesage);
    }

    // 常用角色-添加账号密码
    LoginRoleInfo roleInfo = Main.frame.getRoleInfo();
    roleInfo.setAccount(loginUserBean.getUsername());
    roleInfo.setPassword(loginUserBean.getPassword());
  }
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(this.background.getImage(), 0, 0, 1024, 720, (ImageObserver)null);
    this.btnLogopic.draw(g);
    this.btnRegister.draw(g);

    this.btnRemId.draw(g);
    this.btnPwdPro.draw(g);
    this.btnDropdown.draw(g);
    //this.btnCancel.draw(g);
    //this.btnExit.draw(g);
    if (this.btngouzi1 != null) {
      this.btngouzi1.draw(g);
    }
    if (this.btngouzi2 != null) {
      this.btngouzi2.draw(g);
    }
    if (this.msg != null) {
      g.setColor(Color.red);
      g.drawString(this.msg, 386, 308);
    }
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setFont(UIUtils.TEXT_FONT81);
    g2d.setColor(Color.red);
    //g2d.drawString("记住密码", 415, 370);
    //g2d.drawString("密码保护", 420, 170);

    ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
    Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
    Configure configure9 = item.get(new BigDecimal(9));
// 版本号
     if("2013".equals(configure9.getZqsld())){

       this.btnLogin.setBounds(435, 405, 140, 35);
       this.btnLogin.draw(g);

     }else {

       this.btnLogin.setBounds(0, 0, 0, 0);
      g2d.drawString("您的版本号不一致，请更新版本！", 388, 250);

    }

//    Graphics2D g3d = (Graphics2D)g;
//    g3d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//    g3d.setFont(UIUtils.TEXT_FONT85);
//    g3d.setColor(Color.red);

//    g3d.drawString("本作品仅供个人学习交流，非商业用途，请于下载后24小时内删除", 10, 710);
  }
  public SpriteBtn getBtngouzi1() {
    return this.btngouzi1;
  }
  
  public void setBtngouzi1(SpriteBtn btngouzi1) {
    this.btngouzi1 = btngouzi1;
  }
  
  public SpriteBtn getBtngouzi2() {
    return this.btngouzi2;
  }
  
  public void setBtngouzi2(SpriteBtn btngouzi2) {
    this.btngouzi2 = btngouzi2;
  }
  
  public KeyView getKeyView() {
    return this.keyView;
  }
  
  public void setKeyView(KeyView keyView) {
    this.keyView = keyView;
  }
  
  public RegisterView getRegisterView() {
    return this.registerView;
  }
  
  public void setRegisterView(RegisterView registerView) {
    this.registerView = registerView;
  }
  
  public DownView getDownView() {
    return this.downView;
  }
  
  public void setDownView(DownView downView) {
    this.downView = downView;
  }
  
  public String getMsg() {
    return this.msg;
  }
  
  public void setMsg(String msg) {
    this.msg = msg;
  }
  
  public IphoneView getIphoneView() {
    return this.iphoneView;
  }
  
  public void setIphoneView(IphoneView iphoneView) {
    this.iphoneView = iphoneView;
  }
}
